﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ServiceLayer;
using HelpSystem;

namespace Transportions
{
    public partial class fMain : Form
    {
        CDataBase DataBase = null;

        private bool Connect()
        {
            //Проверяем подключение к БД
            if (DataBase == null)
            {
                DataBase = new CDataBase();
                DataBase.StateChanged += new CDataBase.DelegateStateChange(OnConnectionStateChanged);
            }
            else
            {
                return DataBase.Reconnect(); 
            }
            return true;
        }

        public fMain()
        {
            InitializeComponent();
            Documentation.MainForm = this;
            Documentation.LoadErrorCodes("error.xml");
            DataBase = new CDataBase();
            DataBase.StateChanged += new CDataBase.DelegateStateChange(OnConnectionStateChanged);
            DataBase.Open();
        }

        private void OnConnectionStateChanged(ConnectionState NewState)
        {
            switch (NewState)
            {
                case ConnectionState.Open:
                    {
                        sslDataBaseStatus.Text = "Состояние: подключено";
                        sslDataBaseStatus.ForeColor = Color.Green;
                    }
                    break;
                case ConnectionState.Closed:
                    {
                        sslDataBaseStatus.Text = "Состояние: отключено";
                        sslDataBaseStatus.ForeColor = Color.Red;
                    }
                    break;
            }
        }

        private void miExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void miOptionsDB_Click(object sender, EventArgs e)
        {
            CConfig DBConfig = new CConfig(DataBase);
            if (DBConfig != null)
            {
                DBConfig.Update();
                DBConfig.ShowDialog();
            }
        }

        private void miTransportTypes_Click(object sender, EventArgs e)
        {
            FormTansportType fTransportType = new FormTansportType(DataBase);
            fTransportType.ShowDialog();
        }

        private void fMain_Load(object sender, EventArgs e)
        {
        }

        private void miAbout_Click(object sender, EventArgs e)
        {

        }

        private void miHelp_Click(object sender, EventArgs e)
        {
            Help.ShowHelp(null, "help.chm", HelpNavigator.KeywordIndex, "EC0001");
        }

        private void Pages_Selected(object sender, TabControlEventArgs e)
        {
            if (e.TabPage == tpTransport)
                {
                    Transport.Rows.Clear();
                    DataTable NewRows = DataBase.LoadTableTransport();
                    foreach (DataRow Row in NewRows.Rows)
                    {
                        Transport.Rows.Add(Row["transport_id"],
                                           Row["gov_number"],
                                           Row["transport_type"],
                                           Row["VIN"],
                                           ((DateTime) Row["date_to"]).ToString("d"));
                    }
                }
        }

        private void Transport_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            foreach (DataGridViewRow row in Transport.SelectedRows)
            {
                CTransport SelectedTransport = new CTransport(DataBase, Convert.ToInt32(row.Cells["transport_id"].Value));
                SelectedTransport.Show();
            }
        }
    }
}
